package com.evon.yardmanagement.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="ROLE")
@AttributeOverride( name="id", column = @Column(name="ROLE_ID") )
public class Role extends BaseModel implements Serializable {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column(name ="ROLE_NAME")
	private String name;

	@ManyToOne
	@JoinColumn(name="COMPANY_ID")
	private Vendor owningCompany;
	
    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="ROLE_ACTION", 
                joinColumns={@JoinColumn(name="ROLE_ID")}, 
                inverseJoinColumns={@JoinColumn(name="ACTION_ID")})
	private List<Action> allowedAction;



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Vendor getOwningCompany() {
		return owningCompany;
	}

	public void setOwningCompany(Vendor owningCompany) {
		this.owningCompany = owningCompany;
	}

	public List<Action> getAllowedAction() {
		return allowedAction;
	}
	
	public Set<String> getAllowedActionName() {
		if(allowedAction != null && !allowedAction.isEmpty()){
			Set<String> actions = new HashSet<String>();
			for(Action action : allowedAction){
				actions.add(action.getName());
			}
			return actions;
		}
		return null;
	}

	public void setAllowedAction(List<Action> allowedAction) {
		this.allowedAction = allowedAction;
	}
}
